package com.hanyee.percentProcess.widget;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PercentProgressView extends View {

	private static final String TAG = PercentProgressView.class.getSimpleName();
	private static final int HANDLER_START_DRAW = 0;
	private ArrayList<Category> mCategories;
	private Paint mPaint;
	private float left;
	private float top;
	private float bottom;
	private float width;
	private float[] destPercent;
	private float[] increase;
	boolean isDrawing;

	public PercentProgressView(Context context) {
		super(context);
		init();
	}

	public PercentProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public PercentProgressView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		Log.d(TAG, "PercentProgressView constructed!!");
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mCategories = new ArrayList<PercentProgressView.Category>();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		left = 0;
		top = 0;
		width = getWidth();
		bottom = getHeight();
		if(!isDrawing)return ;
		// Draw every percent part
		for (int i = mCategories.size() - 1; i >= 0; i--) {
			int color = mCategories.get(i).color;
			mPaint.setColor(color);
			canvas.drawRect(left, top, width * increase[i], bottom, mPaint);
		}
		if(increase[increase.length-1]==1)
			isDrawing =false;
	}

	public class Category {
		int color;
		float percentage;

		public Category(int color, float percentage) {
			this.color = color;
			this.percentage = percentage;
		}
	}

	public void setPercent(ArrayList<Category> categories) {
		if (null == categories || categories.size() == 0) {
			Log.d(TAG, "data is null !");
			return;
		}
		if (isDrawing) {
			Log.w(TAG, "No is Drawing!!!!!!wait for 1 seconds");
			return;
		}
		float percentages = 0;
		for (Category category : categories) {
			percentages += category.percentage;
		}
		if (percentages > 1) {
			throw new RuntimeException("你设置的百分比综合超过了1");
		}
		mCategories.clear();
		this.mCategories.addAll(categories);
		accelerated = 0.01f;
		destPercent = new float[mCategories.size()];
		increase = new float[mCategories.size()];
		float sum = 0;
		for (int i = 0; i < mCategories.size(); i++) {
			sum = destPercent[i] = 1 * mCategories.get(i).percentage + sum;
			Log.d(TAG, "destPercent[" + (i) + "]=" + destPercent[i]);
		}
		handler.sendEmptyMessage(HANDLER_START_DRAW);
	}
	float  accelerated=0.01f;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HANDLER_START_DRAW:
				isDrawing = true;
				for (int i = 0; i < destPercent.length; i++) {
					increase[i] += destPercent[i] * accelerated ;
				}
				accelerated =0.001f+accelerated;
				if (increase[increase.length - 1] >= destPercent[destPercent.length - 1]) {
					for(int i=0;i<increase.length;i++){
						increase[i] = destPercent[i];
					}
					invalidate();
				} else {
					invalidate();
					sendEmptyMessageDelayed(HANDLER_START_DRAW, 10);
				}
				break;
			default:
				break;
			}
		};
	};

	protected void onDetachedFromWindow() {
		handler.removeMessages(HANDLER_START_DRAW);
	};

}
